$(function(){
	//回车搜索
	$(document).keypress(function(e) {
		//回车键事件
		if(e.which == 13) {
			var searchKeyVal = $("#searchKeyVal").val();
			$("#sync_search_key").val(searchKeyVal);
			$("#searchForm").submit();
		}
	});
	
	//排序按钮
	$("a[name='sortBtn']").each(function(){
		$(this).on("click", function(){
			var tag = $(this).attr("tag");
			var sort = $(this).attr("sort");
			console.log(tag+":"+sort);
			if (!sort) {
				sort = "ASC";
			} else if (sort == "ASC") {
				sort = "DESC";
			} else {
				sort = "";
			}
			$("#sync_"+tag).val(sort);
			//$("#isSort").val("true");
			$("#searchForm").submit();
		});
	});
	
	// 上拉加载更多
	$(window).on('scroll',function(){
		if($(window).scrollTop()>=$(document).height()-$(window).height()){

			var page_count = $("#sync_page_count").val();
			var current_page = $("#sync_page_num").val();
			if(parseInt(current_page) >= parseInt(page_count)){
				//layer.msg('已经到底了',{time:500});
				return false;
			}
			var params = "requestType=sync";
			$("input[id^='sync_']").each(function(){
				if($(this).attr("pname") == "pageNum"){
					params = params + "&tmpCurrentPage="+(parseInt($(this).val())+1);
				}else{
					params = params + "&"+$(this).attr("pname")+"="+$(this).val();
				}
			});
			// 搜索潮品
			var url = rootPath + "/wx/my/myShare";
			$.ajax({
				type : "post",
				url : url,
				data : params,
				datatype : "json",
				success : function(jsonObject) {
					var json = $.parseJSON(jsonObject);
					$("#sync_page_num").val(json.currentPage);
					if(json.recordList){
						for(var i = 0 ; i < json.recordList.length; i++){
							var shareInfo = json.recordList[i];
//							var headImg = shareInfo.wxHeadImage;
//							if (headImg.indexOf('http') < 0) {
//								headImg = imgRootURL + headImg + ossImgZoomWX
//							}
							var shareInfo_str = "<div class='article' id=''>" +
							"<div class='box1'>" +
							"<div class='pic'>" +
							"<img src='"+ imgRootURL + shareInfo.sharedProductImagePath + ossImgZoomWX +"' onclick='toShareDetailPage("+shareInfo.id+");'>" +
							"</div>" +
							"<div class='item-info'>" +
							"<h2 onclick='toShareDetailPage("+shareInfo.id+");'>"+shareInfo.shareTitle+"</h2>" +
							"<dl>" +
							"<dt>"+
								"<a href='javascriopt:void(0);' class='zan'>" +
									"<em class='current'></em>" +
									"<span>"+shareInfo.likeQuantity+"</span>" +
								"</a>"+
							"</dt>" +
							"<dt>"+
								"<a href='javascriopt:void(0);' class='fav'>" +
									"<em class='current'></em>" +
									"<span>"+shareInfo.favoriteQuantity+"</span>" +
								"</a>"+
							"</dt>" +
							"<dt>"+
								"<a href='javascriopt:void(0);' class='com'>" +
									"<em class='current'></em>" +
									"<span>"+shareInfo.commentQuantity+"</span>" +
								"</a>"+
							"</dt>" +
							"</dl>" +
							"</div>" +
							"</div>" +
							"</div>";
//							$("#share_info_list").append($(product_str));
							var wallDiv = $("#share_info_list").find(".wall-column")[i%2];
							$(wallDiv).append($(shareInfo_str));
						}
					}
				},
				error : function() {
					layer.msg('查询分享信息失败',{time:1000});
				}
			});
		}
	});
});
/**
 * 跳转至详情页
 * @param id
 */
function toShareDetailPage(id){
	window.location.href = rootPath + "/wx/share/detail?id="+id;
}